import 'dart:convert';
import 'dart:io';

import 'package:car_baidu/core/constant/data/data.dart';
import 'package:car_baidu/core/constant/data/share_data_contants.dart';
import 'package:car_baidu/core/service/http_config.dart';
import 'package:car_baidu/core/service/http_request.dart';
import 'package:dio/dio.dart';

class LLLoginRequest {
  static loginRequest(
    String phone,
    String password, {
    InterceptorsWrapper? wrapper,
  }) async {
    String appType = "1";
    if (Platform.isIOS) {
      appType = "2";
    }
    // String udid = await FlutterUdid.udid;
    Map<String, dynamic> params = {
      "strLoginName": phone,
      "strPassWord": password,
      "appType": appType,
      "registrationId": "123",
    };
    Response loginResponse = await LLHttpRequest.httpRequest(
        HttpConfig.loginUrl,
        params: params,
        wrapper: wrapper);
    if (loginResponse.data["success"] == true) {
      Map<String, dynamic> carListParams = {
        "strPhone": loginResponse.data["strPhone"]
      };
      // 登陆成功获取 车辆以及个人数据
      Response carResponse = await LLHttpRequest.httpRequest(
          HttpConfig.carListUrl,
          params: carListParams,
          wrapper: wrapper);
      if (carResponse.data["success"] == true) {
        LLShareData.setString(
            LLShareDataContants.carListInfo, json.encode(carResponse.data));
      }
      await userInfoRequest(loginResponse.data["strPersonID"],
          wrapper: wrapper);

      // 数据存储 + 数据模型重组
      LLShareData.setString(
          LLShareDataContants.loginInfo, json.encode(loginResponse.data));
    }
  }

  static userInfoRequest(
    String strPersonID, {
    InterceptorsWrapper? wrapper,
  }) async {
    Map<String, dynamic> userInfoParams = {"strPersonID": strPersonID};
    Response userInfoResponse = await LLHttpRequest.httpRequest(
        HttpConfig.userInfoUrl,
        params: userInfoParams,
        wrapper: wrapper);

    if (userInfoResponse.data["success"] == true) {
      LLShareData.setString(
          LLShareDataContants.userInfo, json.encode(userInfoResponse.data));
    }
  }
}
